package org.softmed.rest.editor.comps.browser;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import org.restlet.Client;
import org.restlet.data.Protocol;
import org.restlet.data.Response;
import org.softmed.rest.editor.EditorUtil;
import org.softmed.rest.editor.commons.SwingUtil;
import org.softmed.rest.editor.commons.URIProvider;
import org.softmed.rest.editor.uri.URIUtil;
import org.softmed.rest.editor.xml.XStreamConfigurator;

import com.thoughtworks.xstream.XStream;

public class PagedListPanel extends JPanel implements ActionListener,
		MouseListener {
	boolean settingValues = false;
	PagedList pagedList;

	InfoBar infoBar = new InfoBar();
	ItemList itemList;
	InfoBar bottomInfoBar = new InfoBar();

	XStream streamer = XStreamConfigurator.getXStream();
	Client client = new Client(Protocol.HTTP);

	String baseURI;

	URIProvider rootAdminURIProvider;
	private SelectionListener listener;

	public PagedListPanel(URIProvider rootAdminURIProvider) {
		this.rootAdminURIProvider = rootAdminURIProvider;
		itemList = new ItemList();
		// XStreamConfigurator.configurePagedListComplete(streamer);

		SwingUtil.setSize(this, EditorUtil.restBrowserWidth, 640);
		MigLayout layout = new MigLayout("left,insets 0");
		setLayout(layout);

		add(infoBar, "wrap 0");
		add(itemList, "wrap 0");
		add(bottomInfoBar);

		infoBar.getPages().addActionListener(this);
		infoBar.getNavBar().addNavButtonsActionListener(this);

		bottomInfoBar.getPages().addActionListener(this);
		bottomInfoBar.getNavBar().addNavButtonsActionListener(this);

		itemList.setEditorsMouseListener(this);

	}

	public void setBaseClassURI(String baseURI) throws Throwable {
		this.baseURI = baseURI;
		openURI(baseURI);
	}

	public void setPagedList(PagedList plist) throws Throwable {
		settingValues = true;
		pagedList = plist;
		infoBar.setPagedList(plist);
		// navBar.setPagedList(plist);
		itemList.setPagedList(plist);
		bottomInfoBar.setPagedList(plist);
		// bottomBar.setPagedList(plist);
		settingValues = false;
	}

	protected void openURI(String uri) throws Throwable {
		try {
			EditorUtil.showUpdatePanel();
			Response response = client.get(uri);
			EditorUtil.hideUpdatePanel();
			if (response.getStatus().getCode() != 200)
				throw new RuntimeException("Couldn't GET at uri: " + uri);
			String xml = response.getEntity().getText();
			PagedList plist = (PagedList) streamer.fromXML(xml);

			URIUtil.decodeAmpersands(plist);

			setPagedList(plist);
		} catch (Throwable t) {
			EditorUtil.hideUpdatePanel();
			t.printStackTrace();
			JOptionPane.showMessageDialog(this, t.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
			throw t;
		}

	}

	public void refresh() throws Throwable {
		if (pagedList != null)
			openURI(baseURI + "&page=" + pagedList.getCurrentPageNumber());
		else
			openURI(baseURI);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (settingValues)
			return;

		try {
			Object source = e.getSource();
			if (source == infoBar.getPages()) {
				Integer selectedPaged = (Integer) infoBar.getPages()
						.getSelectedItem();
				openURI(baseURI + "&page=" + selectedPaged);
				return;
			}

			if (source == bottomInfoBar.getPages()) {
				Integer selectedPaged = (Integer) bottomInfoBar.getPages()
						.getSelectedItem();
				openURI(baseURI + "&page=" + selectedPaged);
				return;
			}

			if (source == infoBar.getNavBar().getFirst()) {
				openURI(pagedList.getFirstURI());
				return;
			}

			if (source == infoBar.getNavBar().getPrevious()) {
				openURI(pagedList.getPreviousURI());
				return;
			}

			if (source == infoBar.getNavBar().getNext()) {
				openURI(pagedList.getNextURI());
				return;
			}

			if (source == infoBar.getNavBar().getLast()) {
				openURI(pagedList.getLastURI());
				return;
			}

			if (source == bottomInfoBar.getNavBar().getFirst()) {
				openURI(pagedList.getFirstURI());
				return;
			}

			if (source == bottomInfoBar.getNavBar().getPrevious()) {
				openURI(pagedList.getPreviousURI());
				return;
			}

			if (source == bottomInfoBar.getNavBar().getNext()) {
				openURI(pagedList.getNextURI());
				return;
			}

			if (source == bottomInfoBar.getNavBar().getLast()) {
				openURI(pagedList.getLastURI());
				return;
			}

		} catch (Throwable t) {
			EditorUtil.hideUpdatePanel();
			t.printStackTrace();
			JOptionPane.showMessageDialog(this, t.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	public void setSelectionListener(SelectionListener listener) {
		this.listener = listener;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getClickCount() != 2)
			return;

		Object object = itemList.getSelectedObject();
		if (object != null)
			listener.selected(object);

	}

	@Override
	public void mouseEntered(MouseEvent e) {

	}

	@Override
	public void mouseExited(MouseEvent e) {

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		// System.out.println("Pressed! ");
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public InfoBar getInfoBar() {
		return infoBar;
	}

	public void disableButtons() {
		infoBar.disableButtons();
		bottomInfoBar.disableButtons();
	}

}
